//
//  CATransformLayerViewController.m
//  CoreAnimationDemo
//
//  Created by 王亮 on 2017/8/10.
//  Copyright © 2017年 王亮. All rights reserved.
//

#import "CATransformLayerViewController.h"

@interface CATransformLayerViewController ()
@property (nonatomic, strong)UIView *containerView;

@end

@implementation CATransformLayerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.width)];
    self.containerView.backgroundColor = [UIColor grayColor];
    self.containerView.center = self.view.center;
    self.containerView.userInteractionEnabled = YES;
    [self.view addSubview:self.containerView];
    
    //set up the perspective transform
    CATransform3D pt = CATransform3DIdentity;
    pt.m34 = -1/500.0;
    self.containerView.layer.sublayerTransform = pt;
    
    //set up the cube1 and add it
    CATransform3D cube1 = CATransform3DIdentity;
    cube1 = CATransform3DTranslate(cube1, -100, 0, 0);
    CALayer *cube1Layer = [self cubeWithTransform:cube1];
    [self.containerView.layer addSublayer:cube1Layer];
    
    //set up the cube2 and add it
    CATransform3D cube2 = CATransform3DIdentity;
    cube2 = CATransform3DTranslate(cube2, 100, 0, 0);
    cube2 = CATransform3DRotate(cube2, -M_PI_4, 1, 0, 0);
    cube2 = CATransform3DRotate(cube2, -M_PI_4, 0, 1, 0);
    [self.containerView.layer addSublayer:[self cubeWithTransform:cube2]];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (CALayer *)faceWithTransform:(CATransform3D)transform {
    
    CALayer *face = [CALayer layer];
    face.frame = CGRectMake(0, 0, 100, 100);
    
    face.backgroundColor = [UIColor colorWithRed:(arc4random()%256)/255.0 green:(arc4random()%256)/255.0 blue:(arc4random()%256)/255.0 alpha:1.0].CGColor;
    
    face.transform = transform;
    return face;
}

- (CALayer *)cubeWithTransform:(CATransform3D)tf {
    
    //create cube layer
    CATransformLayer *cube = [CATransformLayer layer];
    cube.bounds = CGRectMake(0, 0, 100, 100);
    
    CGSize faceSize = CGSizeMake(100, 100);
    //add face1
    CATransform3D transform = CATransform3DMakeTranslation(0, 0, faceSize.width/2.0);
    [cube addSublayer:[self faceWithTransform:transform]];
    //add face2
    transform = CATransform3DMakeTranslation(faceSize.width/2.0, 0, 0);
    transform = CATransform3DRotate(transform, -M_PI_2, 0, 1, 0);
    [cube addSublayer:[self faceWithTransform:transform]];
    //add face3
    transform = CATransform3DMakeTranslation(0, 0, -faceSize.width/2.0);
    [cube addSublayer:[self faceWithTransform:transform]];
    //add face4
    transform = CATransform3DMakeTranslation(-faceSize.width/2.0, 0, 0);
    transform = CATransform3DRotate(transform, M_PI_2, 0, 1, 0);
    [cube addSublayer:[self faceWithTransform:transform]];
    //add face5
    transform = CATransform3DMakeTranslation(0, -faceSize.width/2.0, 0);
    transform = CATransform3DRotate(transform, M_PI_2, 1, 0, 0);
    [cube addSublayer:[self faceWithTransform:transform]];
    //add face6
    transform = CATransform3DMakeTranslation(0, faceSize.width/2.0, 0);
    transform = CATransform3DRotate(transform, -M_PI_2, 1, 0, 0);
    [cube addSublayer:[self faceWithTransform:transform]];
    
    //设置为中心，是为了统一灭点，最后再通过tf移动到想要的地方，不要直接设置position到指定的位置
    CGSize contentVIewSize = self.containerView.bounds.size;
    cube.position = CGPointMake(contentVIewSize.width/2.0, contentVIewSize.height/2.0);
    
    cube.transform = tf;
    return cube;
}

@end
